class SoundGenerator {
    constructor() {
        this.audioContext = null;
        this.initAudioContext();
    }

    // 初始化音频上下文
    initAudioContext() {
        try {
            this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
        } catch (e) {
            console.warn('Web Audio API not supported');
        }
    }

    // 生成音调
    generateTone(frequency = 440, duration = 0.1, type = 'sine') {
        if (!this.audioContext) return null;

        const oscillator = this.audioContext.createOscillator();
        const gainNode = this.audioContext.createGain();
        
        oscillator.connect(gainNode);
        gainNode.connect(this.audioContext.destination);
        
        oscillator.frequency.value = frequency;
        oscillator.type = type;
        
        // 设置音量包络
        gainNode.gain.setValueAtTime(0, this.audioContext.currentTime);
        gainNode.gain.linearRampToValueAtTime(0.3, this.audioContext.currentTime + 0.01);
        gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + duration);
        
        oscillator.start(this.audioContext.currentTime);
        oscillator.stop(this.audioContext.currentTime + duration);
        
        return { oscillator, gainNode };
    }

    // 生成武器发射音效
    generateWeaponSound(weaponType = 'fireball') {
        switch(weaponType) {
            case 'fireball':
                return this.generateTone(800, 0.1, 'sawtooth');
            case 'lightning':
                return this.generateTone(1200, 0.05, 'square');
            case 'ice':
                return this.generateTone(600, 0.15, 'triangle');
            case 'laser':
                return this.generateTone(1000, 0.08, 'sine');
            case 'orbital':
                return this.generateTone(400, 0.2, 'sine');
            case 'shield':
                return this.generateTone(300, 0.3, 'triangle');
            default:
                return this.generateTone(800, 0.1, 'sine');
        }
    }

    // 生成敌人死亡音效
    generateEnemyDeathSound() {
        const sounds = [];
        
        // 生成多个音调组成爆炸音效
        sounds.push(this.generateTone(200, 0.2, 'sawtooth'));
        sounds.push(this.generateTone(150, 0.3, 'square'));
        sounds.push(this.generateTone(100, 0.4, 'triangle'));
        
        return sounds;
    }

    // 生成升级音效
    generateUpgradeSound() {
        const sounds = [];
        
        // 生成上升音调
        sounds.push(this.generateTone(400, 0.1, 'sine'));
        setTimeout(() => sounds.push(this.generateTone(600, 0.1, 'sine')), 100);
        setTimeout(() => sounds.push(this.generateTone(800, 0.1, 'sine')), 200);
        
        return sounds;
    }

    // 生成经验获得音效
    generateExperienceSound() {
        return this.generateTone(1000, 0.05, 'sine');
    }

    // 生成按钮点击音效
    generateButtonClickSound() {
        return this.generateTone(600, 0.05, 'square');
    }

    // 生成背景音乐（简单的循环音调）
    generateBackgroundMusic() {
        if (!this.audioContext) return null;

        const oscillator = this.audioContext.createOscillator();
        const gainNode = this.audioContext.createGain();
        
        oscillator.connect(gainNode);
        gainNode.connect(this.audioContext.destination);
        
        oscillator.frequency.value = 220;
        oscillator.type = 'sine';
        
        // 设置低音量
        gainNode.gain.setValueAtTime(0.1, this.audioContext.currentTime);
        
        oscillator.start(this.audioContext.currentTime);
        
        return { oscillator, gainNode };
    }

    // 生成所有音效
    generateAllSounds() {
        const sounds = {};
        
        // 武器音效
        sounds.fireball = () => this.generateWeaponSound('fireball');
        sounds.lightning = () => this.generateWeaponSound('lightning');
        sounds.ice = () => this.generateWeaponSound('ice');
        sounds.laser = () => this.generateWeaponSound('laser');
        sounds.orbital = () => this.generateWeaponSound('orbital');
        sounds.shield = () => this.generateWeaponSound('shield');
        
        // 游戏音效
        sounds.enemyDeath = () => this.generateEnemyDeathSound();
        sounds.upgrade = () => this.generateUpgradeSound();
        sounds.experience = () => this.generateExperienceSound();
        sounds.buttonClick = () => this.generateButtonClickSound();
        sounds.backgroundMusic = () => this.generateBackgroundMusic();
        
        return sounds;
    }

    // 播放音效
    playSound(soundFunction) {
        if (soundFunction && this.audioContext) {
            try {
                soundFunction();
            } catch (e) {
                console.warn('Failed to play sound:', e);
            }
        }
    }

    // 停止所有音效
    stopAllSounds() {
        if (this.audioContext) {
            this.audioContext.close();
            this.initAudioContext();
        }
    }
}

// 导出到全局
window.SoundGenerator = SoundGenerator; 